'''
Created on 2011-3-

@author: Shek
Name: CloseUpMaker
Version:V1
'''

from Tkinter import *
from FileDialog import * 
import Image


s=1
x1=[0,0,0]
y1=[0,0,0]
x2=0
y2=0

root = Tk() 
#  Canvas
fd = LoadFileDialog(root)
a = fd.go()
pho = Image.open(a)
img_w,img_h=pho.size
print (img_w,' ',img_h)

img = PhotoImage(file = a)

def transf(Minrate):
    w2=w*Minrate/100
    h2=h*Minrate/100
    return w2,h2


w=img_w
h=img_h
while w>=img_w:
    w=input("With=?")
    if w>=img_w:
        print ("With is over ",img_w,"Please input again/n")
while h>=img_h:
    h=input("High=?")
    if h>=img_h:
        print ("High is over",img_h,"Please input again/n")    

hrate=img_h*100/h
wrate=img_w*100/w

F_width,F_height=transf(min(hrate,wrate))

cv = Canvas(root,
            width = img_w,
            height = img_h,
            bg = 'white')

def printCoords(event): 
    global s,x1,y1,x2,y2,F_width,F_height,w,h
    if s==1:
        x2=event.x
        y2=event.y
        x1[1]=event.x
        y1[1]=event.y
        dx=event.x
        dy=event.y
        print("first",x1[1],y1[1],dx,dy)
    else:
        x1[2]=event.x
        y1[2]=event.y
        dx=x1[2]-x1[1]
        dy=y1[2]-y1[1]    

    cv.move(rt,dx,dy)
    s=s+1
    if s>2:
        x1[1]=x1[2]
        y1[1]=y1[2]
        s=2
    nimg=pho.crop((event.x,event.y,event.x+F_width,event.y+F_height))
    nimg=nimg.resize((w,h))
    nimg = nimg.convert('RGB')
    nimg.save('newsVer1.jpg')
    cv.pack()

cv.create_image((img_w/2,img_h/2),image = img)
rt=cv.create_rectangle(0,0,F_width,F_height,
                    outline='red',
                    dash=1,
                    width=2,
                    stipple = 'gray12', 
                    tags=('r1','r2','r3'),
                    fill='blue') 

cv.tag_bind('r1','<B1-Motion>',printCoords)

cv.pack() 
root.mainloop() 
#  PhotoImage GIF image img 
